package com.gosophia.conferenceManagement.notification.ws.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.common.service.ConferenceHandleService;
import com.gosophia.conferenceManagement.confPreparation.service.ConfPreparationService;
import com.gosophia.conferenceManagement.notification.dto.SendNotificationJobDataDTO;
import com.gosophia.conferenceManagement.notification.entity.SendNotificationJobData;
import com.gosophia.conferenceManagement.notification.ws.SendNotificationWebService;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.service.TaskManagementService;
import com.gosophia.taskManagement.service.impl.TaskManagementServiceImpl;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophia.userManagement.service.UserManagementService;
/**
 * 发送通知Webservice实现
 * @author gaoweiguang
 * @since  2011-4-02
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Service("sendNotificationWebService")
@Transactional
public class SendNotificationWebServiceImpl implements
		SendNotificationWebService {

    @Autowired
    private TaskManagementService taskManagementService;
    
    @Autowired
    private UserManagementService userManagementService;
    
    @Autowired
	@Qualifier("confPreparationService")
    private ConfPreparationService confPreparationService;
    
	@Autowired
	@Qualifier("sendNotificationService")
	private ConferenceHandleService conferenceHandleService;

	@Override
	public SendNotificationJobDataDTO findSendNotificationJobDataByJobIdAndSendUserId(
			Long jobId) throws BusinessException {
		Assert.isTrue(jobId != null, "jobId不能为null");    
		SendNotificationJobData sendNotificationJobData = new SendNotificationJobData();

		sendNotificationJobData = (SendNotificationJobData)taskManagementService.findJobDataDetail(jobId, SendNotificationJobData.class);
		
		SendNotificationJobDataDTO sendNotificationJobDataDTO = new SendNotificationJobDataDTO();
		List<Long> userId = new ArrayList<Long>();
		for(UserInfo user:sendNotificationJobData.getSendUsers()) {
			userId.add(user.getUserInfoId());
		}
		sendNotificationJobDataDTO.setReceiveUserId(userId);
		sendNotificationJobDataDTO.setOperatorId(sendNotificationJobData.getJob().getOperator().getUserInfoId());
		sendNotificationJobDataDTO.setSendNotificationType(sendNotificationJobData.getSendNotificationType());
		sendNotificationJobDataDTO.setSendTime(sendNotificationJobData.getSendTime());
		sendNotificationJobDataDTO.setJobId(jobId);
		return sendNotificationJobDataDTO;
	}

	@SuppressWarnings("deprecation")
	@Override
	public void sendMail(SendNotificationJobDataDTO sendNotificationJobDataDTO) throws BusinessException {
		    Assert.notNull(sendNotificationJobDataDTO.getJobId(), "jobId不能为null");
	        Assert.notNull(sendNotificationJobDataDTO, "data不能为null");
	        Assert.notNull(sendNotificationJobDataDTO.getOperatorId(),"进行流转时操作人Id不能为空");
	        SendNotificationJobData sendNotificationJobData = new SendNotificationJobData();
	        sendNotificationJobData.setJob(taskManagementService.findJobDetailByJobId(sendNotificationJobDataDTO.getJobId()));
	        sendNotificationJobData.setSendNotificationType(sendNotificationJobDataDTO.getSendNotificationType());
	        sendNotificationJobData.setSendTime(new Date());
	        List<UserInfo> userInfos = new ArrayList<UserInfo>();
	        if(sendNotificationJobDataDTO.getReceiveUserId()!=null && sendNotificationJobDataDTO.getReceiveUserId().size()!=0) {
	           for(Long userId:sendNotificationJobDataDTO.getReceiveUserId()) {
	        	   UserInfo userInfo = new UserInfo();
	        	   userInfo = userManagementService.findUserInfoByUserInfoId(userId);
	        	   userInfos.add(userInfo);
	           }
	        	
	        }
	        sendNotificationJobData.setSendUsers(userInfos);
	        sendNotificationJobData.setTurningAction(sendNotificationJobDataDTO.getTurningAction());
	        conferenceHandleService.HandleConferenceTurning(sendNotificationJobDataDTO.getJobId(), sendNotificationJobDataDTO.getOperatorId(), sendNotificationJobData);
	        Job job = taskManagementService.findJobDetailByJobId(sendNotificationJobDataDTO.getJobId());
	       
	        if("1".equals(sendNotificationJobDataDTO.getSendNotificationType())) {
	        	 //取消预通知任务
		       
		        Job jobpreNotifation = taskManagementService.findJobByMissionIdAndJobType(job.getMission().getMissionId(), "预通知发送");
		        if(jobpreNotifation==null) {
		        	Job jobCreate = taskManagementService.findJobByMissionIdAndJobType(job.getMission().getMissionId(), "预通知起草");
		        	UserInfo opreator = userManagementService.findUserInfoByUserInfoId(sendNotificationJobDataDTO.getOperatorId());
		        	if(jobCreate!=null) {
		        		if(jobCreate.getNextJob()==null) {
		        			((TaskManagementServiceImpl) taskManagementService).cancelJob(jobCreate, opreator);
		        		}
		        			
		        	}
		        	
		           Job jobSubmit = taskManagementService.findTodoJobByMissionIdAndJobType(job.getMission().getMissionId(), "预通知审批");
		           if(jobSubmit!=null) {	
		        	  if(jobSubmit.getNextJob()==null){
		        		  ((TaskManagementServiceImpl) taskManagementService).cancelJob(jobSubmit, opreator);
		        	  }
		           }
		        	
		           Job jobModify = taskManagementService.findTodoJobByMissionIdAndJobType(job.getMission().getMissionId(), "预通知修改");
		           if(jobModify!=null) {
		        	   if(jobModify.getNextJob()==null){
			        		  ((TaskManagementServiceImpl) taskManagementService).cancelJob(jobModify, opreator);
			        	  }
		           }
		        	
		           Job jobGetBack = taskManagementService.findTodoJobByMissionIdAndJobType(job.getMission().getMissionId(), "预通知审批取回");
		           if(jobGetBack!=null) {
		        	   if(jobGetBack.getNextJob()==null){
			        		  ((TaskManagementServiceImpl) taskManagementService).cancelJob(jobGetBack, opreator);
			        	  }
		           }
		        	
		        	Job jobFinlize = taskManagementService.findJobByMissionIdAndJobType(job.getMission().getMissionId(), "预通知定稿");
		        	if(jobFinlize!=null) {
		        		if(jobFinlize.getNextJob()==null) {
		        			((TaskManagementServiceImpl) taskManagementService).cancelJob(jobFinlize, opreator);	
		        		}
		        			
		        	}
			        Job jobSend = taskManagementService.findTodoJobByMissionIdAndJobType(job.getMission().getMissionId(), "预通知发送");
			           if(jobSend!=null) {
			        	   if(jobSend.getNextJob()==null){
				        		  ((TaskManagementServiceImpl) taskManagementService).cancelJob(jobSend, opreator);
				        	  }
			           }
		        	
		        }
		        // 会务管理
		        //获取会务的jobId
		        Job jobMeetNotifation = taskManagementService.findJobByMissionIdAndJobType(job.getMission().getMissionId(), "会务准备");
		        confPreparationService.addNotificationFeedbackList(jobMeetNotifation.getJobId(), userInfos);
	        }


	}

}
